﻿using Infinity.BusinessLayer;
using Infinity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Infinity.Areas.Managerial.Controllers
{
    public class CourseController : Controller
    {
        InfinityEntities context = new InfinityEntities();
        //
        // GET: /Managerial/Course/
        public ActionResult Index()
        {
            List<CourseList> list = context.Courses.Select(c => new CourseList
            {
                Course_Name = c.Course_Name,
                CourseID = c.CourseID
            }).ToList();
            return View(list);
        }
        [HttpPost]
        public ActionResult Index(string searchBy, string searchfor)
        {
            List<CourseViewModel> list = context.Courses.Select(c => new CourseViewModel
            {
                Course_Name = c.Course_Name,
              //  Exam=c.Exam,
               // Material=c.course_material_certificate
              //  CategoryID = c.CategoryID
            }).ToList();

            if (searchBy == "name")
            {
                list = list.Where(i => i.Course_Name.ToLower().Contains(searchfor.ToLower()) || searchfor == null).ToList();
            }
            if (searchBy == "id")
            {
                list = list.Where(i => i.CourseID == int.Parse(searchfor) || searchfor == null).ToList();
            }

            return View(list);
        }
        public ActionResult create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult create(CourseViewModel cts)
        {
            return View();
        }
        public ActionResult AddNewOutline(int send)
        {
            OutlineViewModel o = new OutlineViewModel();
            o.create();
            return PartialView ("_Outline", o.create());
        }
        public ActionResult NewCourse()
        {
            CourseViewModel Crs = new CourseViewModel();
            Random rnd = new Random();
            Crs.CourseID = rnd.Next(1, 13000);
            ViewBag.catList = context.Categories.ToList();
            ViewBag.nationality = context.Nationalities.ToList();
            ViewBag.group_type = context.Group_type.ToList();

            OutlineViewModel outl = new OutlineViewModel { CourseID = Crs.CourseID, Outline_Name = "", OutlineID = rnd.Next(1, 13000) };
            Crs.Outlines.Add(outl);
            CourseGroup_typeViewModel gt = new CourseGroup_typeViewModel { CourseID = Crs.CourseID};
            //gt.create();
            Crs.course_grouptype.Add(gt);
            return View(Crs);
        }
        [HttpPost]
        public ActionResult NewCourse(CourseViewModel crs)
        {
            //if (ModelState.IsValid)
            //{
          Course c = new Course();
            c.CourseID = crs.CourseID;
            c.Course_Name = crs.Course_Name;
            c.CategoryID = 22;//crs.CategoryID;
            c.Official_crs_hrs = crs.Official_crs_hrs;
            c.Exam = true;//crs.Exam;
          context.Courses.Add(c);
         // context.SaveChanges();
          //  course_material_certificate CM = new course_material_certificate();
          //      CM.material = crs.Material;
          //      CM.certificate = crs.Certificate;
          //      CM.CourseID = crs.CourseID;
          //context.course_material_certificate.Add(CM);
          //context.SaveChanges();
          //   List<Outline> listOL =crs.Outlines.Select(l=> new Outline {
          //    CourseID=l.CourseID,
          //    OutlineID=l.OutlineID,
          //    Outline_Name=l.Outline_Name
             
          //   }).ToList();
           
          //  foreach (var item in listOL)
          //  {
          //context.Outlines.Add(item);
          //  }

          //  List<course_grouptype> listgt = crs.course_grouptype.Select(l => new course_grouptype
          //  {
          //      CourseID = l.CourseID,
          //      GrouptypeID = l.GrouptypeID,
          //      NationalityID = l.NationalityID,
          //      Price =l.Price
          //  }).ToList();

          //  foreach (var item in listgt)
          //  {
          //context.course_grouptype.Add(item);
          //  }
              //  context.SaveChanges();//save in db
                return RedirectToAction("Index");
            //}
          //  else return View(crs);
        }

    }
}
